#!/usr/bin/env perl
#===============================================================================
#
#         FILE:  installqla.pl
#
#        USAGE:  ./installqla.pl  
#
#  DESCRIPTION:  a script for installing qlogic failover driver
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  razone <zoujichun3166@gmail.com>
#      COMPANY:  
#      VERSION:  
#      CREATED:  2010年10月03日 15时13分28秒
#     REVISION:  ---
#       CODING:  utf8
#===============================================================================

use strict;
use warnings;
use File::Path 'rmtree';

sub usage {
    print "$0 qla_driver_tgzfile\n";
    exit 1;
}

# Check your permission
if ("$<" != "0") {
    print "Please run this script as root!\n";
    exit 1;
}

# Check the argument
if (@ARGV != 1){
    &usage;
}

# Decompression, build
my $tmpdir = "/tmp/$$";
eval {
    mkdir "$tmpdir",0755;
    !system "tar","xzf",$ARGV[0],"-C",$tmpdir or die "Invalued tgz file!";
    chdir "$tmpdir/qlogic";
    system "./drvrsetup";
    chdir glob "qla2xxx-8*";
    !system "./extras/build.sh","install" or die "Build Error: $!";
};
if ($@) {
    chdir "/";
    rmtree("$tmpdir");
    print "An error occurred: $@";
    exit 1;
}
chdir "/";
rmtree("$tmpdir");

# Modify /etc/modprobe.conf
$ARGV[0] = "/etc/modprobe.conf";
$^I = ".old";
while (<>) {
    next if /qla2\d+/; #clear qla2432, etc
    print;
}

## Regenerate initrd.img
my $kernelver = `uname -r`;
chomp($kernelver);
rename "/boot/initrd-$kernelver.img","/boot/initrd-$kernelver.img.old" or die "Rename error: $!";
eval {
    system "depmod","-a";
    system "mkinitrd","/boot/initrd-$kernelver.img","$kernelver";
};
if ($@) {
    print "An error occurred: $@";
    rename "/boot/initrd-$kernelver.img.old","/boot/initrd-$kernelver.img";
    exit 1;
}
print "Installation completed, You should reboot now!\n";
